/**
  ******************************************************************************
  * @file	SysRTQueue.c
  * @author 	Peter Chen
  * @version
  * @date
  * @brief
  ******************************************************************************
  * @attention
  *
  *
  *
  *
  *
  * <h2><center>&copy; COPYRIGHT 2014 INMOTION TECHNOLOGIES CO.,LTD.</center></h2>
  ******************************************************************************
  */
  
#include "SysRTQueue.h"
#include "ucos_ii.h"

/** @addtogroup SingleWheel_Project
  * @{
  */

/** @addtogroup Algorithms
  * @{
  */ 
  
/** @defgroup SysRTQueue
  * @{
  */

/** @defgroup SysRTQueue_Private_TypesDefinitions
  * @{
  */
/**
  * @}
  */

/** @defgroup SysRTQueue_Private_Defines
  * @{
  */  
/**
  * @}
  */

/** @addtogroup SysRTQueue_Private_Macros
  * @{
  */

/**
  * @}
  */

/** @addtogroup SysRTQueue_Private_Variables
  * @{
  */
/**
  * @}
  */
  
/** @addtogroup SysRTQueue_Private_FunctionPrototypes
  * @{
  */
/**
  * @}
  */

/** @addtogroup SysRTQueue_Private_Functions
  * @{
  */ 
/**
  * @}
  */  
/**
  * @brief
  * @param  queue:
  * @retval None
  */
void InitSysRTQ(SysRTQueue *queue, SysRealTimeInfo *buf, u16 bufcnt)
{
	u16 i;
	SysRealTimeInfo tmp = {0};
	
	queue->h = 0;
	queue->t = 0;
	queue->maxsize = bufcnt;
	queue->pBuf = buf;
	for(i = 0 ; i < bufcnt ; i++)
		queue->pBuf[i] = tmp;
}

/**
  * @brief
  * @param  queue:
  * @note   
  * @retval None
  */
u16 SysRTQPush(SysRTQueue *queue, SysRealTimeInfo element)
{
	u16 nRealTail;
	OS_CPU_SR  cpu_sr = 0;

	OS_ENTER_CRITICAL();
	//(queue->tail + 1) % queue->maxsize
	nRealTail = queue->t + 1;
	if(nRealTail >= queue->maxsize )
		nRealTail -= queue->maxsize;

	//is full ?
	if(nRealTail == queue->h ) {
		OS_EXIT_CRITICAL();
		return 0;
	}
	//OK push data
	queue->pBuf[queue->t] = element;
	//queue->tail = (queue->tail + 1) % queue->maxsize;
	queue->t = nRealTail;

	OS_EXIT_CRITICAL();
	return 1;
}

/**
  * @brief
  * @param  CANx:
  * @note   
  * @retval SysRealTimeInfo element
  */
SysRealTimeInfo SysRTQPop(SysRTQueue *queue)
{
	SysRealTimeInfo pRet = {0};
	OS_CPU_SR  cpu_sr = 0;
	
	OS_ENTER_CRITICAL();
	// is empty?
	if(queue->t == queue->h)
	{
		OS_EXIT_CRITICAL();
		return pRet;
	}
	// OK pop data
	pRet = queue->pBuf[queue->h];
	//queue->h = (queue->h + 1) % queue->maxsize;
	queue->h++;
	if(queue->h >= queue->maxsize)
		queue->h -= queue->maxsize;

	OS_EXIT_CRITICAL();
	return pRet;
}

/**
  * @brief
  * @param  CANx:
  * @note   
  * @retval Configure result:
  *		- 0	not full
  *		- 1	full
  */
u16 SysRTQFull(SysRTQueue *queue)
{
	u16 nRealTail , ret;
	OS_CPU_SR  cpu_sr = 0;

	OS_ENTER_CRITICAL();
	nRealTail = queue->t + 1;
	if(nRealTail >= queue->maxsize )
		nRealTail -= queue->maxsize;

	//is full ?
	if(nRealTail == queue->h )
		ret = 1;	
	else
		ret = 0;

	OS_EXIT_CRITICAL();
	return ret;
}

/**
  * @}
  */
  
/**
  * @}
  */  
  
/**
  * @}
  */

/********* (C) COPYRIGHT 2014 INMOTION TECHNOLOGIES CO.,LTD. ***END OF FILE***/
